<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: class.breadcrumb.php 135 2013-09-22 07:30:55Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 135 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-22 07:30:55 +0000 (Sun, 22 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

class breadcrumb {
    var $_trail;
    var $sep;
    var $lenght;

    function __construct() {
		$this->reset();
		$this->sep = BREADCRUMB_SEPERATOR;
		$this->lenght = BREADCRUMB_MAX_LENGTH;
    }

    function reset() {
		$this->_trail = array();
    }
    
	function add($title, $link = '') {
		$total_length = 0;
		foreach ($this->_trail as $el)
			$total_length += strlen($el['title']);

		$total_length += strlen($title);
		if ($total_length > $this->lenght) {
			$all_elements = count($this->_trail);
			$max_length   = round($this->lenght / $all_elements)-3;
			if ($max_length < 3)
				$max_length = 3;
			foreach ($this->_trail AS &$el) {
				if (strlen($el['title']) > $max_length+3) {
					$el['title']  = substr($el['title'], 0, $max_length);
					$el['title'] .= '...';
				}
			}
		}
		$this->_trail[] = array('title' => $title, 'full_title' => $title, 'link' => $link);
	}

	function trail() {
		global $schema;
		
		$trail_string = '';
		
		$rn_is = $schema->ItemScope('breadcrumb');
		$rn_ip = $schema->ItemProp('breadcrumb');
		
		for ($i=0, $n=sizeof($this->_trail); $i<$n; $i++) {
			if (isset($this->_trail[$i]['link']) && not_null($this->_trail[$i]['link'])) {
				$trail_string .= '<span'.$rn_is.'><a'.$rn_ip['url'].' href="'.$this->_trail[$i]['link'].'" title="'.$this->_trail[$i]['full_title'].'" class="headerNavigation"><span'.$rn_ip['title'].'>'.$this->_trail[$i]['title'].'</span></a></span>';
			} else
				$trail_string .= $this->_trail[$i]['title'];
			
			if (($i+1) < $n)
				$trail_string .= ' '.$this->sep.' ';
		}
		return $trail_string;
	}
}